<?php
session_start();
require_once '../classes/Database.php';
require_once '../classes/ProductManager.php';
require_once '../classes/OrderManager.php';
require_once '../classes/CDKManager.php';
require_once '../classes/EasyPay.php';
require_once '../classes/SettingsHelper.php';

// 检查登录状态
if (!isset($_SESSION['admin_logged_in']) || !$_SESSION['admin_logged_in']) {
    header('Location: login.php');
    exit;
}

$productManager = new ProductManager();
$orderManager = new OrderManager();
$cdkManager = new CDKManager();
$easyPay = new EasyPay();

// 获取统计数据
$productStats = $productManager->getProductStatistics();
$orderStats = $orderManager->getOrderStatistics();
$cdkStats = $cdkManager->getCDKStatistics();
$paymentStats = $easyPay->getPaymentStatistics(7);

// 获取最近订单
$recentOrders = $orderManager->getRecentOrders(10);
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>后台管理 - <?php echo htmlspecialchars(getSiteName()); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <link href="assets/admin.css" rel="stylesheet">
</head>
<body>
    <!-- 顶部导航栏（移动端显示） -->
    <div class="top-navbar d-flex align-items-center justify-content-between">
        <button class="sidebar-toggle">
            <i class="fas fa-bars"></i>
        </button>
        <h5 class="mb-0"><?php echo htmlspecialchars(getSiteName()); ?> - 后台管理</h5>
        <div class="text-muted small">
            <i class="fas fa-user me-1"></i><?php echo htmlspecialchars($_SESSION['admin_username']); ?>
        </div>
    </div>

    <!-- 侧边栏 -->
    <div class="sidebar">
        <div class="sidebar-header">
            <h4 class="mb-3">
                <i class="fas fa-cogs me-2"></i><span><?php echo htmlspecialchars(getSiteName()); ?></span>
            </h4>
            <p class="text-muted mb-0 small">欢迎, <?php echo htmlspecialchars($_SESSION['admin_username']); ?></p>
        </div>
        
        <nav class="nav flex-column">
            <a href="index.php" class="nav-link active" data-tooltip="仪表盘">
                <i class="fas fa-tachometer-alt"></i><span>仪表盘</span>
            </a>
            <a href="products.php" class="nav-link" data-tooltip="商品管理">
                <i class="fas fa-box"></i><span>商品管理</span>
            </a>
            <a href="cdks.php" class="nav-link" data-tooltip="CDK管理">
                <i class="fas fa-gift"></i><span>CDK管理</span>
            </a>
            <a href="orders.php" class="nav-link" data-tooltip="订单管理">
                <i class="fas fa-shopping-cart"></i><span>订单管理</span>
            </a>
            <a href="payments.php" class="nav-link" data-tooltip="支付记录">
                <i class="fas fa-credit-card"></i><span>支付记录</span>
            </a>
            <a href="settings.php" class="nav-link" data-tooltip="系统设置">
                <i class="fas fa-cog"></i><span>系统设置</span>
            </a>
            <a href="logout.php" class="nav-link" data-tooltip="退出登录">
                <i class="fas fa-sign-out-alt"></i><span>退出登录</span>
            </a>
        </nav>
    </div>

    <!-- 主内容区 -->
    <div class="main-content">
        <!-- 页面标题 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>
                <i class="fas fa-tachometer-alt me-2"></i>仪表盘
            </h2>
            <div class="text-muted">
                <i class="fas fa-calendar me-2"></i><?php echo date('Y年m月d日 H:i:s'); ?>
            </div>
        </div>

        <!-- 统计卡片 -->
        <div class="row mb-4">
            <div class="col-md-3">
                <div class="card stat-card card-product">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <h5 class="card-title text-muted">商品总数</h5>
                                <h2 class="text-primary"><?php echo $productStats['total_products'] ?? 0; ?></h2>
                                <small class="text-muted">上架: <?php echo $productStats['active_products'] ?? 0; ?></small>
                            </div>
                            <i class="fas fa-box stat-icon text-primary"></i>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-md-3">
                <div class="card stat-card card-order">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <h5 class="card-title text-muted">订单总数</h5>
                                <h2 class="text-danger"><?php echo $orderStats['total_orders'] ?? 0; ?></h2>
                                <small class="text-muted">今日: <?php echo $orderStats['today_orders'] ?? 0; ?></small>
                            </div>
                            <i class="fas fa-shopping-cart stat-icon text-danger"></i>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-md-3">
                <div class="card stat-card card-cdk">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <h5 class="card-title text-muted">CDK总数</h5>
                                <h2 class="text-success"><?php echo $cdkStats['total_cdks'] ?? 0; ?></h2>
                                <small class="text-muted">可用: <?php echo $cdkStats['available_cdks'] ?? 0; ?></small>
                            </div>
                            <i class="fas fa-gift stat-icon text-success"></i>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="col-md-3">
                <div class="card stat-card card-payment">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <h5 class="card-title text-muted">支付总额</h5>
                                <h2 class="text-warning">¥<?php echo number_format($paymentStats['total_amount'] ?? 0, 2); ?></h2>
                                <small class="text-muted">成功率: <?php echo $paymentStats['success_rate'] ?? 0; ?>%</small>
                            </div>
                            <i class="fas fa-credit-card stat-icon text-warning"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 最近订单 -->
        <div class="row">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0">
                            <i class="fas fa-clock me-2"></i>最近订单
                        </h5>
                    </div>
                    <div class="card-body">
                        <?php if (empty($recentOrders)): ?>
                            <div class="text-center py-4">
                                <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                                <p class="text-muted">暂无订单</p>
                            </div>
                        <?php else: ?>
                            <div class="table-responsive">
                                <table class="table table-hover">
                                    <thead>
                                        <tr>
                                            <th>订单号</th>
                                            <th>商品</th>
                                            <th>金额</th>
                                            <th>状态</th>
                                            <th>时间</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($recentOrders as $order): ?>
                                            <tr>
                                                <td>
                                                    <a href="orders.php?action=view&id=<?php echo $order['id']; ?>" class="text-decoration-none">
                                                        <?php echo substr($order['order_no'], 0, 8) . '...'; ?>
                                                    </a>
                                                </td>
                                                <td><?php echo htmlspecialchars($order['product_name']); ?></td>
                                                <td>¥<?php echo number_format($order['total_amount'], 2); ?></td>
                                                <td>
                                                    <?php
                                                    $statusColors = [
                                                        'pending' => 'warning',
                                                        'completed' => 'success',
                                                        'failed' => 'danger',
                                                        'cancelled' => 'secondary'
                                                    ];
                                                    $statusTexts = [
                                                        'pending' => '待支付',
                                                        'completed' => '已完成',
                                                        'failed' => '支付失败',
                                                        'cancelled' => '已取消'
                                                    ];
                                                    ?>
                                                    <span class="badge bg-<?php echo $statusColors[$order['status']]; ?>">
                                                        <?php echo $statusTexts[$order['status']]; ?>
                                                    </span>
                                                </td>
                                                <td><?php echo date('m-d H:i', strtotime($order['created_at'])); ?></td>
                                            </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            
            <!-- 快捷操作 -->
            <div class="col-md-4">
                <div class="card">
                    <div class="card-header bg-success text-white">
                        <h5 class="mb-0">
                            <i class="fas fa-bolt me-2"></i>快捷操作
                        </h5>
                    </div>
                    <div class="card-body">
                        <div class="d-grid gap-2">
                            <a href="products.php?action=add" class="btn btn-primary">
                                <i class="fas fa-plus me-2"></i>添加商品
                            </a>
                            <a href="cdks.php?action=import" class="btn btn-success">
                                <i class="fas fa-upload me-2"></i>导入CDK
                            </a>
                            <a href="orders.php" class="btn btn-info">
                                <i class="fas fa-list me-2"></i>查看所有订单
                            </a>
                            <a href="settings.php" class="btn btn-warning">
                                <i class="fas fa-cog me-2"></i>系统设置
                            </a>
                        </div>
                    </div>
                </div>
                
                <!-- 系统信息 -->
                <div class="card mt-4">
                    <div class="card-header bg-secondary text-white">
                        <h5 class="mb-0">
                            <i class="fas fa-info-circle me-2"></i>系统信息
                        </h5>
                    </div>
                    <div class="card-body">
                        <p><strong>PHP版本：</strong><?php echo PHP_VERSION; ?></p>
                        <p><strong>MySQL版本：</strong><?php echo '5.7+'; ?></p>
                        <p><strong>服务器时间：</strong><?php echo date('Y-m-d H:i:s'); ?></p>
                        <p><strong>最后登录：</strong><?php echo date('Y-m-d H:i:s'); ?></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="assets/admin.js"></script>
</body>
</html>
